{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div align=\"right\"><i>Peter Norvig, June 2015<br>(Updated April 2024)</i></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Let's Code About Bike Locks"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The [June 15, 2015 post](http://bikesnobnyc.blogspot.com/2015/06/lets-get-this-show-on-road-once-we-all.html) on [*Bike Snob NYC*](http://bikesnobnyc.blogspot.com) leads with \"*Let's talk about bike locks.*\" Here's what I want to talk about: in my local bike shop, I saw a combination  lock called *WordLock&reg;*,\n",
    "which replaces digits  with  letters. There are 4 discs in the lock, each of which has 10 distinct letters.  I classified this as a Fred lock,\n",
    "\"[Fred](http://bikesnobnyc.blogspot.com/2014/06/a-fred-too-far.html)\" being the term for an amateurish cyclist with inappropriate equipment. I played around with it and got this:\n",
    "\n",
    "![](http://norvig.com/ipython/fredbuns.jpg)\n",
    "\n",
    "\n",
    "Naturally I set  the other locks on the rack to FRED BUNS as well. But I have questions ... \n",
    "\n",
    "# Questions\n",
    "\n",
    "1. How many words can the WordLock&reg; make?\n",
    "3. Can a lock with different letters on the discs make more words? \n",
    "4. How many words can be made simultaneously? The photo above shows the words \"FRED\" and \"BUNS,\" but \"SOMN\" is not a word.\n",
    "5. Is it a coincidence that the phrase \"FRED BUNS\" appears, or was it planted there by  WordLock&reg; designers? \n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Preliminaries\n",
    "\n",
    "First, set the stage with some (1) imports, (2) constants, and (3) type definitions:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from   collections import Counter\n",
    "from   typing      import *\n",
    "from   functools   import lru_cache\n",
    "import itertools\n",
    "import random \n",
    "import re\n",
    "import textwrap\n",
    "\n",
    "wordlock = ('SPHMTWDLFB', 'LEYHNRUOAI', 'ENMLRTAOSK', 'DSNMPYLKTE') # The lock in the photo above\n",
    "ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' # The 26 letters\n",
    "DISCS    = 4     # A lock has 4 discs.\n",
    "LETTERS  = 10    # A disc has 10 letters.\n",
    "\n",
    "Letter   = str   # A single letter from the ALPHABET.\n",
    "Disc     = str   # A sequence of letters (joined into a string) forms a disc.\n",
    "Lock     = tuple # A tuple of discs forms a lock.\n",
    "Word     = str   # A 4-letter string that is in the list of valid words. (Some strings are non-words.)\n",
    "Regex    = str   # A regular expression (used to find all the words that can be made by a lock)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# The Word List\n",
    "\n",
    "I happen to have  a file of  four-letter words (no, not *[that](http://en.wikipedia.org/wiki/Four-letter_word)* kind of four-letter word). It is the union of an official Scrabble&reg; word list with a list of proper names. The following shell command tests if the file has already been downloaded to the local directory and if not, fetches it from the web:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "! [ -e words4.txt ] || curl -O http://norvig.com/ngrams/words4.txt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I will use this word list two ways: `WORDSTR` is a big string of words separated by newlines.  `WORDS` is a list of the individual words in the file:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "WORDSTR = open('words4.txt').read()\n",
    "WORDS   = WORDSTR.split()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4360"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(WORDS)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here is a random sampling of the words:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['STOW',\n",
       " 'DRUM',\n",
       " 'AIDS',\n",
       " 'CUSS',\n",
       " 'BAYS',\n",
       " 'COSH',\n",
       " 'DEED',\n",
       " 'PHON',\n",
       " 'KARA',\n",
       " 'ANOA']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.seed(1234) # for reproducability\n",
    "\n",
    "random.sample(WORDS, LETTERS)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Question 1: How Many Words can the WordLock® make?\n",
    "\n",
    "My approach:\n",
    "- Given a lock, I need to compare it against a list of valid words to see which words can be made by the lock.\n",
    "- I  think of this as a matching problem: what words match the pattern described by the lock.\n",
    "- Python's `re` module is good at matching.\n",
    "  - I'll define `regex` to create a regular expression that represents a lock's 10<sup>4</sup> possible combinations.\n",
    "  - I'll define `words_from(lock)`  to match the regular expression against all the words in the word list.\n",
    "  - I can do this in just one call to `re.findall`.\n",
    "- I'll define `word_count` to count the number of words a lock makes, and cache the results (to save time on future calls)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def regex(lock: Lock) -> Regex: \n",
    "    \"\"\"A regular expression describing all 10**4 combinations that this lock can make.\"\"\"\n",
    "    return cat(('[' + disc + ']') for disc in lock)\n",
    "\n",
    "def words_from(lock: Lock, wordstr=WORDSTR) -> List[Word]: \n",
    "    \"\"\"A list of all valid words that can be made by this lock.\"\"\"\n",
    "    return re.findall(regex(lock), wordstr)\n",
    "\n",
    "@lru_cache(None)\n",
    "def word_count(lock) -> int: return len(words_from(lock))\n",
    "\n",
    "cat   = ''.join  # Function to concatenate strings with no space between them.\n",
    "space = ' '.join # Function to join strings with a space between each one."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'[SPHMTWDLFB][LEYHNRUOAI][ENMLRTAOSK][DSNMPYLKTE]'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "regex(wordlock)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can now answer question 1:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1118"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "word_count(wordlock)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That's our answer: WordLock® can make 1,118 words (about 1/4 of the word list).  \n",
    "\n",
    "# Visualizing Results\n",
    "\n",
    "I don't want to print a list with 1,118 lines, so I'll define `show` to print  in a prettier format:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def show(lock: Lock, width=110) -> None:\n",
    "    \"\"\"Show (print) a lock, the words it makes, and the word count.\"\"\"\n",
    "    words = words_from(lock)\n",
    "    print(f'{len(words):,d} words can be made by the lock ({space(lock)}):\\n')\n",
    "    print(textwrap.fill(space(sorted(words)), width))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1,118 words can be made by the lock (SPHMTWDLFB LEYHNRUOAI ENMLRTAOSK DSNMPYLKTE):\n",
      "\n",
      "BAAL BAAS BAKE BALD BALE BALK BALL BALM BALS BAMS BAND BANE BANK BANS BARD BARE BARK BARM BARN BARS BASE BASK\n",
      "BASS BAST BATE BATS BATT BEAD BEAK BEAM BEAN BEAT BEEN BEEP BEES BEET BELL BELS BELT BEND BENE BENS BENT BERK\n",
      "BERM BEST BETS BIAS BIKE BILE BILK BILL BIND BINE BINS BINT BIOS BIRD BIRK BIRL BISE BISK BITE BITS BITT BLAE\n",
      "BLAM BLAT BLED BLET BLOT BOAS BOAT BOLD BOLE BOLL BOLT BOND BONE BONK BONY BOOK BOOM BOON BOOS BOOT BORE BORK\n",
      "BORN BORT BOSK BOSS BOTS BOTT BRAD BRAE BRAN BRAS BRAT BRAY BRED BREE BREN BROS BULK BULL BUMP BUMS BUND BUNK\n",
      "BUNN BUNS BUNT BUOY BURD BURL BURN BURP BURS BURY BUSK BUSS BUST BUSY BUTE BUTS BUTT BYES BYRE BYRL BYTE DAKS\n",
      "DALE DALS DAME DAMN DAMP DAMS DANE DANK DANS DARE DARK DARN DART DATE DEAD DEAL DEAN DEED DEEM DEEP DEES DEET\n",
      "DEKE DELE DELL DELS DELT DEME DEMY DENE DENS DENT DENY DEON DERE DERM DESK DHAK DHAL DIAL DIED DIEL DIES DIET\n",
      "DIKE DILL DIME DIMS DINE DINK DINS DINT DIOL DION DIRE DIRK DIRL DIRT DISK DISS DITE DITS DOAT DOES DOLE DOLL\n",
      "DOLS DOLT DOME DOMS DONE DONS DOOM DORE DORK DORM DORP DORS DORY DOSE DOSS DOST DOTE DOTS DOTY DRAM DRAT DRAY\n",
      "DREE DREK DROP DUAD DUAL DUEL DUES DUET DUKE DULL DULY DUMP DUNE DUNK DUNS DUNT DUOS DURE DURN DUSK DUST DUTY\n",
      "DYAD DYED DYES DYKE DYNE FAKE FALL FAME FANE FANS FARD FARE FARL FARM FART FAST FATE FATS FEAL FEAT FEED FEEL\n",
      "FEES FEET FELL FELT FEME FEMS FEND FENS FEOD FERE FERN FESS FEST FETE FETS FIAT FILE FILL FILM FILS FIND FINE\n",
      "FINK FINN FINS FIRE FIRM FIRN FIRS FIST FITS FLAK FLAM FLAN FLAP FLAT FLAY FLED FLEE FLEY FLOE FLOP FOAL FOAM\n",
      "FOES FOLD FOLK FOND FONS FONT FOOD FOOL FOOT FORD FORE FORK FORM FORT FOSS FRAE FRAP FRAT FRAY FRED FREE FRET\n",
      "FROE FROM FUEL FULL FUME FUMY FUND FUNK FUNS FURL FURS FURY FUSE FUSS FYKE HAED HAEM HAEN HAES HAET HAKE HALE\n",
      "HALL HALM HALT HAME HAMS HAND HANK HANS HANT HARD HARE HARK HARL HARM HARP HART HASP HAST HATE HATS HEAD HEAL\n",
      "HEAP HEAT HEED HEEL HELD HELL HELM HELP HEME HEMP HEMS HENS HENT HERD HERE HERL HERM HERN HERS HEST HETS HIED\n",
      "HIES HIKE HILL HILT HIMS HIND HINS HINT HIRE HISN HISS HIST HITS HOED HOES HOKE HOLD HOLE HOLK HOLM HOLP HOLS\n",
      "HOLT HOLY HOME HOMY HONE HONK HONS HOOD HOOK HOOP HOOT HORN HOSE HOST HOTS HUED HUES HULK HULL HUMP HUMS HUNK\n",
      "HUNS HUNT HURL HURT HUSK HUTS HYMN HYTE LAKE LAKY LALL LAME LAMP LAMS LAND LANE LANK LARD LARK LARS LASE LASS\n",
      "LAST LATE LATS LEAD LEAK LEAL LEAN LEAP LEAS LEEK LEES LEET LEKE LEKS LEND LENS LENT LEON LESS LEST LETS LIAM\n",
      "LIAN LIED LIEN LIES LIKE LILT LILY LIME LIMN LIMP LIMY LINE LINK LINN LINS LINT LINY LION LIRE LISP LIST LITE\n",
      "LITS LOAD LOAM LOAN LOLL LONE LOOK LOOM LOON LOOP LOOS LOOT LORD LORE LORN LORY LOSE LOSS LOST LOTS LUES LUKE\n",
      "LULL LUMP LUMS LUNE LUNK LUNT LUNY LURE LURK LUST LUTE LYAM LYES LYLE LYRE LYSE MAES MAKE MALE MALL MALM MALT\n",
      "MANE MANS MANY MARE MARK MARL MARS MART MARY MASK MASS MAST MATE MATS MATT MEAD MEAL MEAN MEAT MEED MEEK MEET\n",
      "MELD MELL MELS MELT MEME MEMS MEND MERE MERK MERL MESS METE MHOS MIEN MIKE MILD MILE MILK MILL MILS MILT MIME\n",
      "MIND MINE MINK MINT MIRE MIRK MIRS MIRY MISE MISS MIST MITE MITT MITY MOAN MOAS MOAT MOKE MOLD MOLE MOLL MOLS\n",
      "MOLT MOLY MOME MOMS MONK MONS MONY MOOD MOOL MOON MOOS MOOT MORE MORN MORS MORT MOSK MOSS MOST MOTE MOTS MOTT\n",
      "MULE MULL MUMM MUMP MUMS MUNS MUON MURE MURK MUSE MUSK MUSS MUST MUTE MUTS MUTT PALE PALL PALM PALP PALS PALY\n",
      "PAMS PANE PANS PANT PARD PARE PARK PARS PART PASE PASS PAST PATE PATS PATY PEAK PEAL PEAN PEAS PEAT PEED PEEK\n",
      "PEEL PEEN PEEP PEES PEKE PELE PELT PEND PENS PENT PEON PERE PERK PERM PERP PERT PEST PETS PHAT PHON PHOT PIAL\n",
      "PIAN PIAS PIED PIES PIKE PILE PILL PILY PIMP PINE PINK PINS PINT PINY PION PIRN PISS PITS PITY PLAN PLAT PLAY\n",
      "PLED PLOD PLOP PLOT PLOY POEM POET POKE POKY POLE POLL POLS POLY POME POMP POMS POND PONE PONS PONY POOD POOL\n",
      "POON POOP POOS PORE PORK PORN PORT POSE POST POSY POTS PRAM PRAT PRAY PREE PREP PREY PROD PROM PROP PROS PUKE\n",
      "PULE PULL PULP PULS PUMP PUNK PUNS PUNT PUNY PURE PURL PURS PUSS PUTS PUTT PYAS PYES PYRE SAAD SAKE SALE SALL\n",
      "SALP SALS SALT SAME SAMP SAND SANE SANK SANS SARD SARK SASS SATE SEAL SEAM SEAN SEAS SEAT SEED SEEK SEEL SEEM\n",
      "SEEN SEEP SEES SELL SELS SEME SEND SENE SENT SERE SERS SETS SETT SHAD SHAE SHAM SHAT SHAY SHED SHES SHOD SHOE\n",
      "SHOP SHOT SIAL SIKE SILD SILK SILL SILT SIMP SIMS SINE SINK SINS SIRE SIRS SITE SITS SLAM SLAP SLAT SLAY SLED\n",
      "SLOE SLOP SLOT SNAP SNED SNOT SOAK SOAP SOKE SOLD SOLE SOLS SOME SOMS SONE SONS SOOK SOON SOOT SORD SORE SORN\n",
      "SORT SOTS SUED SUES SUET SUKS SULK SUMP SUMS SUNK SUNN SUNS SURD SURE SUSS SYED SYKE SYNE TAEL TAKE TALE TALK\n",
      "TALL TAME TAMP TAMS TANK TANS TAOS TARE TARN TARP TARS TART TASK TASS TATE TATS TEAK TEAL TEAM TEAS TEAT TEED\n",
      "TEEL TEEM TEEN TEES TELE TELL TELS TEMP TEND TENS TENT TERM TERN TESS TEST TETS THAE THAN THAT THEE THEM THEN\n",
      "THEY TIED TIES TIKE TILE TILL TILS TILT TIME TINE TINS TINT TINY TIRE TIRL TITS TOAD TOED TOES TOKE TOLD TOLE\n",
      "TOLL TOME TOMS TONE TONS TONY TOOK TOOL TOOM TOON TOOT TORE TORN TORS TORT TORY TOSS TOST TOTE TOTS TRAD TRAE\n",
      "TRAM TRAP TRAY TREE TREK TRES TRET TREY TROD TROP TROT TROY TULE TUMP TUNE TUNS TURD TURK TURN TUSK TUTS TYEE\n",
      "TYES TYKE TYNE TYRE WAES WAKE WALE WALK WALL WALY WAME WAND WANE WANK WANS WANT WANY WARD WARE WARK WARM WARN\n",
      "WARP WARS WART WARY WASP WAST WATS WATT WEAK WEAL WEAN WEED WEEK WEEL WEEN WEEP WEES WEET WELD WELL WELT WEND\n",
      "WENS WENT WERE WERT WEST WETS WHAM WHAP WHAT WHEE WHEN WHET WHEY WHOM WHOP WILD WILE WILL WILT WILY WIMP WIND\n",
      "WINE WINK WINS WINY WIRE WIRY WISE WISP WISS WIST WITE WITS WOAD WOES WOKE WOKS WOLD WONK WONS WONT WOOD WOOL\n",
      "WOOS WORD WORE WORK WORM WORN WORT WOST WOTS WRAP WREN WUSS WYES WYLE WYND WYNN WYNS WYTE\n"
     ]
    }
   ],
   "source": [
    "show(wordlock)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Aside: How Secure is WordLock?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The WordLock® makes 1,118 words.  You might say that an attacker  would find this lock to be only 11.18% as secure as a 4-digit lock with 10,000 combinations.  But in reality, every cable lock is [vulnerable](https://www.sfbike.org/news/video-how-to-lock-your-bike/) to an attacker who possesses  wire  cutters or a knowledge of lock-picking, so  security is equally terrible for WordLock&reg; and for an equivalent lock with digits instead of letters. You really should use a hardened steel U-lock instead."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Question 2: Can a lock with different letters on the discs make more words?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To make a lock with more words than the original WordLock®, the simplest thing I could think of is a [greedy algorithm](https://en.wikipedia.org/wiki/Greedy_algorithm):\n",
    "1) Consider each of the 4 discs, one at a time.\n",
    "2) Fill each disc with the 10 most common letters (across all possible words) that appear at that position.\n",
    "4) When all 4 discs have been filled, we have a lock.\n",
    "\n",
    "How do we choose the 10 most common letters?  The `Counter.most_common` method can do most of the work:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'SPTBDCLMAR'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def most_common(words, position, n=LETTERS) -> str:\n",
    "    \"\"\"The `n` most common letters in `position` of all the `words`.\"\"\"\n",
    "    counter = Counter(word[position] for word in words)\n",
    "    return cat(letter for (letter, count) in counter.most_common(n))\n",
    "    \n",
    "most_common(WORDS, 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In other words, the ten most common letters from the first position of all the words are SPTBDCLMAR, in that order.\n",
    "\n",
    "The function `greedy_lock` creates a lock with this greedy disc-filling approach. I'm not sure if the best order of discs is left-to-right or right-to-left or something else, so I'll leave the order as a parameter. Order matters because when we choose the 10 letters for one disc, we count \"across all possible words\"; we eliminate any impossible words that don't match one of those 10 letters before we move on to the next disc."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def greedy_lock(words=WORDS, order=range(DISCS)) -> Lock:\n",
    "    \"\"\"Make a lock where we greedily choose the LETTERS best letters for each disc, in order.\"\"\"\n",
    "    lock = DISCS * [Disc()] # Initially a lock of 4 empty discs\n",
    "    for i in order: \n",
    "        # Make lock[i] be a disc whose letters cover the most words, then update `words`\n",
    "        lock[i] = most_common(words, i)\n",
    "        words   = [w for w in words if w[i] in lock[i]]\n",
    "    return Lock(lock)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('SPTBDCLMAR', 'OAIEURLHYN', 'RNALEOTISM', 'SETADNLKYP')"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "greedy_lock()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1177"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "word_count(greedy_lock())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That's an improvement! The original WordLock® makes 1,118 words, so 1,177 is 5% more.\n",
    "\n",
    "# Greedier Algorithm\n",
    "\n",
    "`greedy_lock` might be better if we consider the 4 discs in some other order. Let's try all possible orders and pick the best result:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "def greedier_lock(words=WORDS) -> Lock:\n",
    "    \"\"\"Choose the best greedy lock, considering all possible orderings of discs.\"\"\"\n",
    "    locks = [greedy_lock(words, order) for order in itertools.permutations(range(DISCS))]\n",
    "    return max(locks, key=word_count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('BPTCMSDLGW', 'OAEIURLYHW', 'EARNLIOTSM', 'SETNDALKYP')"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "greedier_lock()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1,235 words can be made by the lock (BPTCMSDLGW OAEIURLYHW EARNLIOTSM SETNDALKYP):\n",
      "\n",
      "BAAL BAAS BAIL BAIT BALD BALE BALK BALL BALS BAMS BAND BANE BANK BANS BARD BARE BARK BARN BARS BASE BASK BASS\n",
      "BAST BATE BATS BATT BEAD BEAK BEAN BEAT BEEN BEEP BEES BEET BELL BELS BELT BEMA BEND BENE BENS BENT BERK BEST\n",
      "BETA BETS BIAS BILE BILK BILL BIMA BIND BINE BINS BINT BIOS BIRD BIRK BIRL BISE BISK BITE BITS BITT BLAE BLAT\n",
      "BLED BLET BLIN BLIP BLOT BOAS BOAT BOIL BOLA BOLD BOLE BOLL BOLT BOND BONE BONK BONY BOOK BOON BOOS BOOT BORA\n",
      "BORE BORK BORN BORT BOSK BOSS BOTA BOTS BOTT BRAD BRAE BRAN BRAS BRAT BRAY BREA BRED BREE BREN BRIA BRIE BRIN\n",
      "BRIS BRIT BROS BULK BULL BUMP BUMS BUNA BUND BUNK BUNN BUNS BUNT BUOY BURA BURD BURL BURN BURP BURS BURY BUSK\n",
      "BUSS BUST BUSY BUTE BUTS BUTT BYES BYRE BYRL BYTE CAEL CAID CAIN CALE CALK CALL CAME CAMP CAMS CANE CANS CANT\n",
      "CARA CARD CARE CARK CARL CARN CARP CARS CART CASA CASE CASK CAST CATE CATS CEES CEIL CELL CELS CELT CENT CERE\n",
      "CESS CETE CHAD CHAP CHAT CHAY CHIA CHID CHIN CHIP CHIS CHIT CHON CHOP CIAN CINE CION CIRE CIST CITE CITY CLAD\n",
      "CLAN CLAP CLAY CLIP CLOD CLON CLOP CLOT CLOY COAL COAT COED COEN COIL COIN COLA COLD COLE COLS COLT COLY COMA\n",
      "COME COMP CONE CONK CONN CONS CONY COOK COOL COON COOP COOS COOT CORA CORD CORE CORK CORN CORS CORY COSS COST\n",
      "COSY COTE COTS CRAP CRED CRIS CRIT CROP CUED CUES CULL CULT CUNT CURD CURE CURL CURN CURS CURT CUSK CUSP CUSS\n",
      "CUTE CUTS CWMS CYAN CYMA CYME CYST DAIS DALE DALS DAME DAMN DAMP DAMS DANA DANE DANK DANS DARE DARK DARN DART\n",
      "DATA DATE DEAD DEAL DEAN DEED DEEP DEES DEET DEIL DELE DELL DELS DELT DEME DEMY DENE DENS DENT DENY DEON DERE\n",
      "DESK DHAK DHAL DIAL DIED DIEL DIES DIET DILL DIME DIMS DINA DINE DINK DINS DINT DIOL DION DIRE DIRK DIRL DIRT\n",
      "DISK DISS DITA DITE DITS DOAT DOES DOIT DOLE DOLL DOLS DOLT DOME DOMS DONA DONE DONS DORE DORK DORP DORS DORY\n",
      "DOSE DOSS DOST DOTE DOTS DOTY DRAT DRAY DREE DREK DRIP DROP DUAD DUAL DUEL DUES DUET DUIT DULL DULY DUMA DUMP\n",
      "DUNE DUNK DUNS DUNT DUOS DURA DURE DURN DUSK DUST DUTY DYAD DYED DYES DYNE GAED GAEL GAEN GAES GAIN GAIT GALA\n",
      "GALE GALL GALS GAMA GAME GAMP GAMS GAMY GANE GAOL GARS GARY GASP GAST GATE GATS GEED GEEK GEES GELD GELS GELT\n",
      "GEMS GENE GENS GENT GEST GETA GETS GHAT GHEE GHIS GIAN GIED GIEN GIES GILD GILL GILT GIMP GINA GINK GINS GIRD\n",
      "GIRL GIRN GIRT GIST GITE GITS GLAD GLED GLEE GLEN GLEY GLIA GLOP GOAD GOAL GOAS GOAT GOES GOLD GONE GOOD GOOK\n",
      "GOON GOOP GOOS GORE GORP GORY GRAD GRAN GRAT GRAY GREE GREY GRID GRIN GRIP GRIT GROK GROT GUAN GUID GULL GULP\n",
      "GULS GUMS GUNK GUNS GUST GUTS GWEN GYMS GYRE LAID LAIN LALL LAMA LAME LAMP LAMS LANA LAND LANE LANK LARA LARD\n",
      "LARK LARS LASE LASS LAST LATE LATS LEAD LEAK LEAL LEAN LEAP LEAS LEEK LEES LEET LEIA LEIS LELA LENA LEND LENS\n",
      "LENT LEON LESS LEST LETS LIAN LIED LIEN LIES LILA LILT LILY LIMA LIME LIMN LIMP LIMY LINA LINE LINK LINN LINS\n",
      "LINT LINY LION LIRA LIRE LISA LISP LIST LITE LITS LOAD LOAN LOID LOIN LOLA LOLL LONE LOOK LOON LOOP LOOS LOOT\n",
      "LORD LORE LORN LORY LOSE LOSS LOST LOTA LOTS LUES LUIS LULL LUMA LUMP LUMS LUNA LUNE LUNK LUNT LUNY LURE LURK\n",
      "LUST LUTE LYES LYLA LYLE LYRA LYRE LYSE MAES MAIA MAID MAIL MAIN MALE MALL MALT MAMA MANA MANE MANS MANY MARA\n",
      "MARE MARK MARL MARS MART MARY MASA MASK MASS MAST MATE MATS MATT MEAD MEAL MEAN MEAT MEED MEEK MEET MELD MELL\n",
      "MELS MELT MEME MEMS MEND MERE MERK MERL MESA MESS META METE MHOS MIEN MILA MILD MILE MILK MILL MILS MILT MIME\n",
      "MINA MIND MINE MINK MINT MIRA MIRE MIRK MIRS MIRY MISE MISS MIST MITE MITT MITY MOAN MOAS MOAT MOIL MOLA MOLD\n",
      "MOLE MOLL MOLS MOLT MOLY MOME MOMS MONA MONK MONS MONY MOOD MOOL MOON MOOS MOOT MORA MORE MORN MORS MORT MOSK\n",
      "MOSS MOST MOTE MOTS MOTT MULE MULL MUMP MUMS MUNS MUON MURA MURE MURK MUSA MUSE MUSK MUSS MUST MUTE MUTS MUTT\n",
      "MYLA MYNA MYRA PAID PAIK PAIL PAIN PALE PALL PALP PALS PALY PAMS PANE PANS PANT PARA PARD PARE PARK PARS PART\n",
      "PASE PASS PAST PATE PATS PATY PEAK PEAL PEAN PEAS PEAT PEED PEEK PEEL PEEN PEEP PEES PEIN PELE PELT PEND PENS\n",
      "PENT PEON PERE PERK PERP PERT PEST PETS PHAT PHIS PHON PHOT PIAL PIAN PIAS PIED PIES PILE PILL PILY PIMA PIMP\n",
      "PINA PINE PINK PINS PINT PINY PION PIRN PISS PITA PITS PITY PLAN PLAT PLAY PLEA PLED PLIE PLOD PLOP PLOT PLOY\n",
      "POET POIS POLE POLL POLS POLY POME POMP POMS POND PONE PONS PONY POOD POOL POON POOP POOS PORE PORK PORN PORT\n",
      "POSE POST POSY POTS PRAT PRAY PREE PREP PREY PROA PROD PROP PROS PULA PULE PULL PULP PULS PUMA PUMP PUNA PUNK\n",
      "PUNS PUNT PUNY PURE PURL PURS PUSS PUTS PUTT PYAS PYES PYIN PYRE SAAD SAID SAIL SAIN SALE SALL SALP SALS SALT\n",
      "SAME SAMP SANA SAND SANE SANK SANS SARA SARD SARK SASS SATE SEAL SEAN SEAS SEAT SEED SEEK SEEL SEEN SEEP SEES\n",
      "SEIS SELL SELS SEME SEND SENE SENT SERA SERE SERS SETA SETS SETT SHAD SHAE SHAT SHAY SHEA SHED SHES SHIN SHIP\n",
      "SHIT SHOD SHOE SHOP SHOT SIAL SILD SILK SILL SILT SIMA SIMP SIMS SINE SINK SINS SIRE SIRS SITE SITS SLAP SLAT\n",
      "SLAY SLED SLID SLIP SLIT SLOE SLOP SLOT SOAK SOAP SOIL SOLA SOLD SOLE SOLS SOMA SOME SOMS SONE SONS SOOK SOON\n",
      "SOOT SORA SORD SORE SORN SORT SOTS SRIS SUED SUES SUET SUIT SULK SUMP SUMS SUNK SUNN SUNS SURA SURD SURE SUSS\n",
      "SWAN SWAP SWAT SWAY SWOP SWOT SYED SYNE TAEL TAIL TAIN TALA TALE TALK TALL TAME TAMP TAMS TANK TANS TAOS TARA\n",
      "TARE TARN TARP TARS TART TASK TASS TATE TATS TEAK TEAL TEAS TEAT TEED TEEL TEEN TEES TELA TELE TELL TELS TEMP\n",
      "TEND TENS TENT TERN TESS TEST TETS THAE THAN THAT THEA THEE THEN THEY THIN THIS TIED TIES TILE TILL TILS TILT\n",
      "TIME TINA TINE TINS TINT TINY TIRE TIRL TITS TOAD TOEA TOED TOES TOIL TOIT TOLA TOLD TOLE TOLL TOME TOMS TONE\n",
      "TONS TONY TOOK TOOL TOON TOOT TORA TORE TORN TORS TORT TORY TOSS TOST TOTE TOTS TRAD TRAE TRAP TRAY TREE TREK\n",
      "TRES TRET TREY TRIP TROD TROP TROT TROY TUIS TULE TUMP TUNA TUNE TUNS TURD TURK TURN TUSK TUTS TWAE TWAS TWAT\n",
      "TWEE TWIN TWIT TWOS TYEE TYES TYIN TYNE TYRE WAES WAIL WAIN WAIT WALE WALK WALL WALY WAME WAND WANE WANK WANS\n",
      "WANT WANY WARD WARE WARK WARN WARP WARS WART WARY WASP WAST WATS WATT WEAK WEAL WEAN WEED WEEK WEEL WEEN WEEP\n",
      "WEES WEET WELD WELL WELT WEND WENS WENT WERE WERT WEST WETS WHAP WHAT WHEE WHEN WHET WHEY WHID WHIN WHIP WHIT\n",
      "WHOA WHOP WILD WILE WILL WILT WILY WIMP WIND WINE WINK WINS WINY WIRE WIRY WISE WISP WISS WIST WITE WITS WOAD\n",
      "WOES WOLD WONK WONS WONT WOOD WOOL WOOS WORD WORE WORK WORN WORT WOST WOTS WRAP WREN WRIT WUSS WYES WYLE WYND\n",
      "WYNN WYNS WYTE\n",
      "CPU times: user 33.8 ms, sys: 1.16 ms, total: 35 ms\n",
      "Wall time: 34.4 ms\n"
     ]
    }
   ],
   "source": [
    "%time show(greedier_lock())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That's another 5% improvement! We've done well, with only two dozen lines of code and under 50 milliseconds of run time. What else can we do?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Hillclimbing Algorithm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The problem with the greedy algorithm is that it does no exploration: at every step it tries one thing, and if it makes a suboptimal choice of letters on one disc, there is no way to undo the choice. I'd like the option to try multiple choices on each disc. But there are too many locks to try all of them: (26 choose 10)<sup>4</sup> ≈ 800 septillion. Even if we only chose from the 14 most common letters for each disc, rather than all 26, that would still be a trillion possible locks. So rather than systematically trying all possible locks, we're left with randomly sampling from possible locks. A process called **hillclimbing** makes random changesd and keeps the changes that help:\n",
    "\n",
    "   1. Start with some lock.\n",
    "   2. Make a random change to some letter(s) in the lock.\n",
    "   3. If the change yields a lock that makes more words, keep the change. Otherwise discard the change.\n",
    "   4. Repeat multiple times.\n",
    "\n",
    "I'm not sure exactly how I want to make a random change, so for now I'll define `changed_lock` to change one random letter in one random disc, but I'll parameterize the function `hillclimb` to accept a different function to allow for different changes (and also to allow a different function for how to score the best lock):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "def changed_lock(lock) -> Lock: \n",
    "    \"\"\"Change one random letter in one random disc in the lock.\"\"\"\n",
    "    # Make a mutable copy of lock. Then change lock2[i]'s `old` letter to a `new` one.\n",
    "    lock2 = list(lock) \n",
    "    i = random.randrange(DISCS)\n",
    "    old: Letter = random.choice(lock[i])\n",
    "    new: Letter = random.choice([L for L in ALPHABET if L not in lock[i]])\n",
    "    lock2[i] = lock2[i].replace(old, new)\n",
    "    return Lock(lock2)\n",
    "\n",
    "def hillclimb(lock, changer=changed_lock, scorer=word_count, repeat=4000) -> Lock:\n",
    "    \"\"\"Starting with `lock`, apply `changer` to make a new lock, keeping it if\n",
    "    `scorer` rates it as better than the previous best lock. Repeat.\"\"\"\n",
    "    best, best_score = lock, scorer(lock)\n",
    "    for _ in range(repeat):\n",
    "        candidate = changer(best)\n",
    "        if scorer(candidate) >= best_score:\n",
    "            best, best_score = candidate, scorer(candidate)\n",
    "    return best"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's see how hillclimbing does:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1,240 words can be made by the lock (SPWMTFDLCB LEYHPRUOAI ENCLRTAOSI DSNHAYLKTE):\n",
      "\n",
      "BAAL BAAS BACH BACK BAIL BAIT BALD BALE BALK BALL BALS BAND BANE BANK BANS BARD BARE BARK BARN BARS BASE BASH\n",
      "BASK BASS BAST BATE BATH BATS BATT BEAD BEAK BEAN BEAT BECK BEEN BEES BEET BELL BELS BELT BEND BENE BENS BENT\n",
      "BERK BEST BETA BETH BETS BIAS BICE BILE BILK BILL BIND BINE BINS BINT BIOS BIRD BIRK BIRL BISE BISK BITE BITS\n",
      "BITT BLAE BLAH BLAT BLED BLET BLIN BLOT BOAS BOAT BOCK BOIL BOLA BOLD BOLE BOLL BOLT BOND BONE BONK BONY BOOK\n",
      "BOON BOOS BOOT BORA BORE BORK BORN BORT BOSH BOSK BOSS BOTA BOTH BOTS BOTT BRAD BRAE BRAN BRAS BRAT BRAY BREA\n",
      "BRED BREE BREN BRIA BRIE BRIN BRIS BRIT BROS BUCK BULK BULL BUNA BUND BUNK BUNN BUNS BUNT BUOY BURA BURD BURL\n",
      "BURN BURS BURY BUSH BUSK BUSS BUST BUSY BUTE BUTS BUTT BYES BYRE BYRL BYTE CACA CAEL CAID CAIN CALE CALK CALL\n",
      "CANE CANS CANT CARA CARD CARE CARK CARL CARN CARS CART CASA CASE CASH CASK CAST CATE CATS CECA CEES CEIL CELL\n",
      "CELS CELT CENT CERE CESS CETE CHAD CHAT CHAY CHIA CHID CHIN CHIS CHIT CHON CIAN CINE CION CIRE CIST CITE CITY\n",
      "CLAD CLAN CLAY CLOD CLON CLOT CLOY COAL COAT COCA COCK COED COEN COIL COIN COLA COLD COLE COLS COLT COLY CONE\n",
      "CONK CONN CONS CONY COOK COOL COON COOS COOT CORA CORD CORE CORK CORN CORS CORY COSH COSS COST COSY COTE COTS\n",
      "CRED CRIS CRIT CUED CUES CULL CULT CUNT CURD CURE CURL CURN CURS CURT CUSK CUSS CUTE CUTS CYAN CYST DACE DAIS\n",
      "DALE DALS DANA DANE DANK DANS DARE DARK DARN DART DASH DATA DATE DEAD DEAL DEAN DECK DEED DEES DEET DEIL DELE\n",
      "DELL DELS DELT DENE DENS DENT DENY DEON DERE DESK DHAK DHAL DIAL DICE DICK DIED DIEL DIES DIET DILL DINA DINE\n",
      "DINK DINS DINT DIOL DION DIRE DIRK DIRL DIRT DISH DISK DISS DITA DITE DITS DOAT DOCK DOCS DOES DOIT DOLE DOLL\n",
      "DOLS DOLT DONA DONE DONS DORE DORK DORS DORY DOSE DOSS DOST DOTE DOTH DOTS DOTY DRAT DRAY DREE DREK DUAD DUAL\n",
      "DUCE DUCK DUCT DUEL DUES DUET DUIT DULL DULY DUNE DUNK DUNS DUNT DUOS DURA DURE DURN DUSK DUST DUTY DYAD DYED\n",
      "DYES DYNE FACE FACT FAIL FAIN FALL FANE FANS FARD FARE FARL FART FASH FAST FATE FATS FEAL FEAT FECK FEED FEEL\n",
      "FEES FEET FELL FELT FEND FENS FEOD FERE FERN FESS FEST FETA FETE FETS FIAT FICE FILA FILE FILL FILS FIND FINE\n",
      "FINK FINN FINS FIRE FIRN FIRS FISH FIST FITS FLAK FLAN FLAT FLAY FLEA FLED FLEE FLEY FLIT FLOE FOAL FOES FOIL\n",
      "FOIN FOLD FOLK FOND FONS FONT FOOD FOOL FOOT FORA FORD FORE FORK FORT FOSS FRAE FRAT FRAY FRED FREE FRET FRIT\n",
      "FROE FUCK FUEL FULL FUND FUNK FUNS FURL FURS FURY FUSE FUSS FYCE LACE LACK LACS LACY LAID LAIN LALL LANA LAND\n",
      "LANE LANK LARA LARD LARK LARS LASE LASH LASS LAST LATE LATH LATS LEAD LEAH LEAK LEAL LEAN LEAS LECH LEEK LEES\n",
      "LEET LEIA LEIS LELA LENA LEND LENS LENT LEON LESS LEST LETS LIAN LICE LICH LICK LIED LIEN LIES LILA LILT LILY\n",
      "LINA LINE LINK LINN LINS LINT LINY LION LIRA LIRE LISA LIST LITE LITS LOAD LOAN LOCA LOCH LOCK LOID LOIN LOLA\n",
      "LOLL LONE LOOK LOON LOOS LOOT LORD LORE LORN LORY LOSE LOSS LOST LOTA LOTH LOTS LUCA LUCE LUCK LUCY LUES LUIS\n",
      "LULL LUNA LUNE LUNK LUNT LUNY LURE LURK LUSH LUST LUTE LYCH LYES LYLA LYLE LYRA LYRE LYSE MACE MACH MACK MACS\n",
      "MACY MAES MAIA MAID MAIL MAIN MALE MALL MALT MANA MANE MANS MANY MARA MARE MARK MARL MARS MART MARY MASA MASH\n",
      "MASK MASS MAST MATE MATH MATS MATT MEAD MEAL MEAN MEAT MEED MEEK MEET MELD MELL MELS MELT MEND MERE MERK MERL\n",
      "MESA MESH MESS META METE METH MHOS MIAH MICA MICE MICK MICS MIEN MILA MILD MILE MILK MILL MILS MILT MINA MIND\n",
      "MINE MINK MINT MIRA MIRE MIRK MIRS MIRY MISE MISS MIST MITE MITT MITY MOAN MOAS MOAT MOCK MOCS MOIL MOLA MOLD\n",
      "MOLE MOLL MOLS MOLT MOLY MONA MONK MONS MONY MOOD MOOL MOON MOOS MOOT MORA MORE MORN MORS MORT MOSH MOSK MOSS\n",
      "MOST MOTE MOTH MOTS MOTT MUCH MUCK MULE MULL MUNS MUON MURA MURE MURK MUSA MUSE MUSH MUSK MUSS MUST MUTE MUTS\n",
      "MUTT MYAH MYCS MYLA MYNA MYRA MYTH PACA PACE PACK PACS PACT PACY PAID PAIK PAIL PAIN PALE PALL PALS PALY PANE\n",
      "PANS PANT PARA PARD PARE PARK PARS PART PASE PASH PASS PAST PATE PATH PATS PATY PEAK PEAL PEAN PEAS PEAT PECH\n",
      "PECK PECS PEED PEEK PEEL PEEN PEES PEIN PELE PELT PEND PENS PENT PEON PERE PERK PERT PEST PETS PHAT PHIS PHON\n",
      "PHOT PIAL PIAN PIAS PICA PICE PICK PICS PIED PIES PILE PILL PILY PINA PINE PINK PINS PINT PINY PION PIRN PISH\n",
      "PISS PITA PITH PITS PITY PLAN PLAT PLAY PLEA PLED PLIE PLOD PLOT PLOY POCK POET POIS POLE POLL POLS POLY POND\n",
      "PONE PONS PONY POOD POOH POOL POON POOS PORE PORK PORN PORT POSE POSH POST POSY POTS PRAT PRAY PREE PREY PROA\n",
      "PROD PROS PUCE PUCK PULA PULE PULL PULS PUNA PUNK PUNS PUNT PUNY PURE PURL PURS PUSH PUSS PUTS PUTT PYAS PYES\n",
      "PYIN PYRE SAAD SACK SACS SAID SAIL SAIN SALE SALL SALS SALT SANA SAND SANE SANK SANS SARA SARD SARK SASH SASS\n",
      "SATE SEAL SEAN SEAS SEAT SECS SECT SEED SEEK SEEL SEEN SEES SEIS SELL SELS SEND SENE SENT SERA SERE SERS SETA\n",
      "SETH SETS SETT SHAD SHAE SHAH SHAT SHAY SHEA SHED SHES SHIN SHIT SHOD SHOE SHOT SIAL SICE SICK SICS SILD SILK\n",
      "SILL SILT SINE SINH SINK SINS SIRE SIRS SITE SITH SITS SLAT SLAY SLED SLID SLIT SLOE SLOT SOAK SOCA SOCK SOIL\n",
      "SOLA SOLD SOLE SOLS SONE SONS SOOK SOON SOOT SORA SORD SORE SORN SORT SOTH SOTS SPAE SPAN SPAS SPAT SPAY SPED\n",
      "SPIK SPIN SPIT SPOT SPRY SRIS SUCH SUCK SUED SUES SUET SUIT SULK SUNK SUNN SUNS SURA SURD SURE SUSS SYCE SYED\n",
      "SYNE TACE TACH TACK TACT TAEL TAIL TAIN TALA TALE TALK TALL TANK TANS TAOS TARA TARE TARN TARS TART TASK TASS\n",
      "TATE TATS TEAK TEAL TEAS TEAT TECH TEED TEEL TEEN TEES TELA TELE TELL TELS TEND TENS TENT TERN TESS TEST TETH\n",
      "TETS THAE THAN THAT THEA THEE THEN THEY THIN THIS TICK TICS TIED TIES TILE TILL TILS TILT TINA TINE TINS TINT\n",
      "TINY TIRE TIRL TITS TOAD TOEA TOED TOES TOIL TOIT TOLA TOLD TOLE TOLL TONE TONS TONY TOOK TOOL TOON TOOT TORA\n",
      "TORE TORN TORS TORT TORY TOSH TOSS TOST TOTE TOTS TRAD TRAE TRAY TREE TREK TRES TRET TREY TROD TROT TROY TUCK\n",
      "TUIS TULE TUNA TUNE TUNS TURD TURK TURN TUSH TUSK TUTS TYCE TYEE TYES TYIN TYNE TYRE WACK WAES WAIL WAIN WAIT\n",
      "WALE WALK WALL WALY WAND WANE WANK WANS WANT WANY WARD WARE WARK WARN WARS WART WARY WASH WAST WATS WATT WEAK\n",
      "WEAL WEAN WEED WEEK WEEL WEEN WEES WEET WELD WELL WELT WEND WENS WENT WERE WERT WEST WETS WHAT WHEE WHEN WHET\n",
      "WHEY WHID WHIN WHIT WHOA WICH WICK WILD WILE WILL WILT WILY WIND WINE WINK WINS WINY WIRE WIRY WISE WISH WISS\n",
      "WIST WITE WITH WITS WOAD WOES WOLD WONK WONS WONT WOOD WOOL WOOS WORD WORE WORK WORN WORT WOST WOTS WREN WRIT\n",
      "WUSS WYCH WYES WYLE WYND WYNN WYNS WYTE\n"
     ]
    }
   ],
   "source": [
    "show(hillclimb(wordlock))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We got up to 1240 words, another improvement!  But can we go beyond that?   \n",
    "\n",
    "I'll create a list of 40 `locks`: the original wordlock, 15 random locks, and 24 greedy locks, one for each permutation of the orders."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "def random_lock() -> Lock:\n",
    "    \"\"\"A lock with randomly-chosen letters.\"\"\"\n",
    "    return Lock(cat(random.sample(ALPHABET, LETTERS)) for dial in range(DISCS))\n",
    "\n",
    "locks  = ([wordlock] + [random_lock() for _ in range(15)] +\n",
    "          [greedy_lock(WORDS, order) for order in itertools.permutations(range(DISCS))])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I'll also define `lock_table`, a little function to summarize the locks and their word counts:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "def lock_table(locks) -> dict: return {lock: word_count(lock) for lock in locks}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{('SPHMTWDLFB', 'LEYHNRUOAI', 'ENMLRTAOSK', 'DSNMPYLKTE'): 1118,\n",
       " ('YAHIQUMEOG', 'KJCXLTRQAB', 'VPTECDBKXO', 'BUVTFXKGSW'): 80,\n",
       " ('XOTNFCBIVS', 'ARWTHKDEJU', 'CKYQDFVWLO', 'TEMADLHYRX'): 115,\n",
       " ('RFGLIXZAED', 'JEOWUVCYIM', 'OCTAVRYISU', 'YOMZCTEJPL'): 178,\n",
       " ('CTIVPWDJXZ', 'REKMFSCJAT', 'KJXHUODPNB', 'IWJUGRZBOC'): 35,\n",
       " ('ILOAHSFKZX', 'XIECBRHAMN', 'SYGRQUPJNZ', 'BMVRKSYCAG'): 142,\n",
       " ('BRVNFOWYLH', 'MWTQHZSFCY', 'TQPGYZXOUK', 'NVXZHWBFUY'): 0,\n",
       " ('KRDMTSBZIA', 'FKJAZYNTPW', 'TWXFCGAJPI', 'XLVOQFHGMN'): 73,\n",
       " ('IKTENRBPSV', 'WFKYQLPJBI', 'IYCFXQUOHW', 'QAKDYFOTMG'): 56,\n",
       " ('INOEVWXSLB', 'ZYXOIWQNSL', 'NUDFMAICPR', 'YNVIZALGWR'): 105,\n",
       " ('DSKRBQEYXZ', 'CPEHRBAXOM', 'QRUNSTMCYV', 'AYDPKVSBIL'): 224,\n",
       " ('RFICNAQEUL', 'YZQPASNLBO', 'QCDZWIONHT', 'TOFMNPKXYW'): 96,\n",
       " ('AKMQVSLOHP', 'VWESKTPMLD', 'INGSRTFPAL', 'WKRIAMFSOY'): 143,\n",
       " ('OBHSIZJAGW', 'BYRENOZUFV', 'DRUWEYOVHK', 'DPNLWIABTC'): 146,\n",
       " ('SIXTNVFQJD', 'TODHWGKILE', 'RKGLUNAJDM', 'CVNHLGIDQF'): 111,\n",
       " ('KHABFQXWRC', 'HVTEFKRUWS', 'HABDOQWGRK', 'VYNJQMWZER'): 76,\n",
       " ('SPTBDCLMAR', 'OAIEURLHYN', 'RNALEOTISM', 'SETADNLKYP'): 1177,\n",
       " ('SPTBDCLMAR', 'OAIEURLHYN', 'RNLEATSCIO', 'SETAYKDNLO'): 1178,\n",
       " ('SPTBDCLMAR', 'OAEIURLHYW', 'ARNELOITSM', 'SETDANLKYP'): 1181,\n",
       " ('SPTBDCLMAR', 'OAEIURLHYW', 'ARNELOITSM', 'SETDNALKYP'): 1181,\n",
       " ('SPTBDCLMAR', 'OAIEURLHYN', 'RNLEATSCIO', 'SETAYNDKLO'): 1178,\n",
       " ('SPTBDCLMAR', 'OAIEURHYLW', 'RNLEAITSOC', 'SETAYNDKLO'): 1181,\n",
       " ('SBPTDCLMRG', 'AOIEURLHYN', 'RNALEOTISM', 'SETDNALKPY'): 1216,\n",
       " ('SBPTDCLMRG', 'AOIEURLHYN', 'NRLEATSCIO', 'SETAKDYNLO'): 1206,\n",
       " ('BPSCTMDLGF', 'AOIEURLHYN', 'RNLEAITOSM', 'SETDLNAKPY'): 1222,\n",
       " ('BPCMSTDLGW', 'AOIEURLHYN', 'RNLEAITOSM', 'SETDANLKYP'): 1226,\n",
       " ('BMPSDTLCRH', 'AOIEURLHYN', 'NRLEASTCIM', 'SETAKDNYLO'): 1207,\n",
       " ('BMPTDCSLFR', 'AOIEURLHYN', 'NRLEATSICD', 'SETAKDNYLO'): 1208,\n",
       " ('SBPTCAMDLG', 'OAEIURLHYW', 'EARNLIOTSM', 'SETDNALKYP'): 1191,\n",
       " ('SBPTCAMDLG', 'OAIEURLHYW', 'EARNLIOTSM', 'SETNDALKYP'): 1191,\n",
       " ('BPSCTMDLGF', 'OAEIURLHYN', 'EARNLIOTSM', 'SETDLNAKPY'): 1222,\n",
       " ('BPCMSTDLGW', 'OAEIURLHYN', 'EARNLIOTSM', 'SETDANLKYP'): 1226,\n",
       " ('SPBATCMDLG', 'OAIEURLHYW', 'EARNLIOTSM', 'SETNDALKYP'): 1191,\n",
       " ('BPTCMSDLGW', 'OAEIURLYHW', 'EARNLIOTSM', 'SETNDALKYP'): 1235,\n",
       " ('STAPBMDLCR', 'OAIEURLHYN', 'RNLEATSCIO', 'SETANDYLKO'): 1178,\n",
       " ('STAPBMDLCR', 'OAIEURHYLW', 'RNLEAITSOC', 'SETANDYLKO'): 1181,\n",
       " ('BMPSDTLCRH', 'AOIEURLYHN', 'NRLEASTCIM', 'SETANDYLKO'): 1207,\n",
       " ('BMPTDCSLFR', 'AOIEURLYHN', 'NRLEATSICD', 'SETANDYLKO'): 1208,\n",
       " ('SBAPTMCDLF', 'OAIEURLHYW', 'NRELAITSOD', 'SETANDYLKO'): 1168,\n",
       " ('BPMTCSDLFW', 'OAIEURLYHN', 'NRELAITSOD', 'SETANDYLKO'): 1197}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lock_table(locks)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now I'll do hillclimbing from each lock and display the results:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 30.9 s, sys: 92.6 ms, total: 31 s\n",
      "Wall time: 31.3 s\n"
     ]
    }
   ],
   "source": [
    "%time hillclimbs = [hillclimb(lock) for lock in locks]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{('SPMWTCDLFB', 'LEYHWRUOAI', 'ENILRTAOSC', 'DSNYAHLKTE'): 1240,\n",
       " ('FPLBSMCDTW', 'IPHLEYURAO', 'ORTEALCISN', 'LYHTKENDSA'): 1240,\n",
       " ('PWSCTLBMFD', 'AROYLPEIHU', 'NOACITESLR', 'TEKADLHSYN'): 1240,\n",
       " ('PMWSTLFBCD', 'LEOHURYAIP', 'SCLATRNIEO', 'YHLAKTEDNS'): 1240,\n",
       " ('CTWLPGDBSM', 'REHAUILYOW', 'AOITREMSNL', 'NKLTDYSPAE'): 1235,\n",
       " ('LBFDMSCPWT', 'LIEPYRHAOU', 'SLERTIAONC', 'NHLDKSAYTE'): 1240,\n",
       " ('BPCWHMTDLS', 'HLPOEIARUY', 'TRSNCEAILP', 'LDATHYNSEK'): 1232,\n",
       " ('PWTMCSBDLF', 'IYEARUPOLH', 'TESROLACNI', 'YLHNSTKAED'): 1240,\n",
       " ('DLTFWPBMSC', 'RWHYUEOLAI', 'NRCLOEAIST', 'YEDASHNTKL'): 1240,\n",
       " ('SFMWDCLPTB', 'WYROIUAELH', 'LOTCEAISNR', 'DNHTKALYSE'): 1240,\n",
       " ('MSFDTBHLPC', 'LIEHRYAWOU', 'ORLNSTECAM', 'AYDTKLSNPE'): 1232,\n",
       " ('BMSPFWDTCL', 'YIEPAHRLUO', 'CEOANSIRLT', 'NKLHADSETY'): 1240,\n",
       " ('FDMLBSCTPW', 'RYEILWUHAO', 'IONARTCSEL', 'KDYLANTSEH'): 1240,\n",
       " ('FBWMCLPSDT', 'WRAEHOIUYL', 'ARLOESTICN', 'DSKLMNAETY'): 1236,\n",
       " ('SWLTCFMPBD', 'YOHURLWIAE', 'IECLONARST', 'AETYLKHDSN'): 1240,\n",
       " ('WPMTFDLSBC', 'HAUELYROIW', 'CAONELISRT', 'SALKEDHYTN'): 1240,\n",
       " ('SPTBDCLMWG', 'OAIEURLHYW', 'RNALEOTISM', 'SETADNLKYP'): 1235,\n",
       " ('SPTBDCLMFW', 'OAIEURLHYP', 'RNLEATSCIO', 'SETAYKDNLH'): 1240,\n",
       " ('SPTBDCLMHF', 'OAEIURLHYW', 'ARNELOCTSM', 'SETDANLKYP'): 1232,\n",
       " ('SPTBDCLMWG', 'OAEIURLHYW', 'ARNELOITSM', 'SETDNALKYP'): 1235,\n",
       " ('SPTBDCLMFW', 'OAIEURLHYW', 'RNLEATSCIO', 'SETAHNDKLY'): 1240,\n",
       " ('SPTBDCLMFW', 'OAIEURHYLW', 'RNLEAITSOC', 'SETAYNDKLH'): 1240,\n",
       " ('SBPTDCLMGW', 'AOIEURLHYW', 'RNALEOTISM', 'SETDNALKPY'): 1235,\n",
       " ('SBPTDCLMWF', 'AOIEURLHYP', 'NRLEATSCIO', 'SETAKDYNLH'): 1240,\n",
       " ('BPSCTMDLWG', 'AOIEURLHYW', 'RNLEAITOSM', 'SETDLNAKPY'): 1235,\n",
       " ('BPCMSTDLGW', 'AOIEURLHYW', 'RNLEAITOSM', 'SETDANLKYP'): 1235,\n",
       " ('BMPSDTLCFW', 'AOIEURLHYW', 'NRLEASTCIO', 'SETAKDNYLH'): 1240,\n",
       " ('BMPTDCSLGW', 'AOIEURLHYW', 'NRLEATSIOM', 'SETAKDNYLP'): 1235,\n",
       " ('SBPTCGMDLW', 'OAEIURLHYW', 'EARNLIOTSM', 'SETDNALKYP'): 1235,\n",
       " ('SBPTCWMDLG', 'OAIEURLHYW', 'EARNLIOTSM', 'SETNDALKYP'): 1235,\n",
       " ('BPSCTMDLGW', 'OAEIURLHYW', 'EARNLIOTSM', 'SETDLNAKPY'): 1235,\n",
       " ('BPCMSTDLGW', 'OAEIURLHYW', 'EARNLIOTSM', 'SETDANLKYP'): 1235,\n",
       " ('SPBWTCMDLG', 'OAIEURLHYW', 'EARNLIOTSM', 'SETNDALKYP'): 1235,\n",
       " ('BPTCMSDLGW', 'OAEIURLYHW', 'EARNLIOTSM', 'SETNDALKYP'): 1235,\n",
       " ('STWPBMDLCF', 'OAIEURLHYW', 'RNLEATSCIO', 'SETANDYLKH'): 1240,\n",
       " ('STFPBMDLCW', 'OAIEURHYLW', 'RNLEAITSOC', 'SETANDYLKH'): 1240,\n",
       " ('BMPSDTLCFH', 'AOIEURLYHW', 'NRLEASTCOM', 'SETANDYLKP'): 1232,\n",
       " ('BMPTDCSLWF', 'AOIEURLYHP', 'NRLEATSICO', 'SETANDYLKH'): 1240,\n",
       " ('SBGPTMCDLW', 'OAIEURLHYW', 'NRELAITSOM', 'SETANDPLKY'): 1235,\n",
       " ('BPMTCSDLFW', 'OAIEURLYHW', 'NRELAITSOC', 'SETANDYLKH'): 1240}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lock_table(hillclimbs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here are the counts of how many times each score occurred:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(1240, 21), (1235, 14), (1232, 4), (1236, 1)]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Counter(_.values()).most_common()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The fact that there are 21 different locks at 1240 (and others that are close) suggested to me that there might be a lock with 1241 or more, and I should search longer. \n",
    "\n",
    "But a discussion with [Matt Chisholm](https://blog.glyphobet.net/faq) changed my thinking. Matt pointed out that some locks that look different are actually the same; they just have the letters within a disc in a different order. I'll define the function `canonical` to put each disc in canonical alphabetical order, and update `lock_table` to do three new things: (1) use the canonical form;  (2) sort the locks by word count; and (3) display along with the word count the [Hamming distance](https://en.wikipedia.org/wiki/Hamming_distance) to a 1240 lock:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "def canonical(lock: Lock) -> Lock:\n",
    "    \"Canonicalize a lock by alphabetizing the letters in each disc.\"\n",
    "    return Lock(cat(sorted(disc)) for disc in lock)\n",
    "    \n",
    "def distance(lock1, lock2) -> int:\n",
    "    return sum(len(set(lock1[i]) - set(lock2[i])) for i in range(DISCS))\n",
    "    \n",
    "lock1240 = canonical(('SPTBDCLMWF', 'OAIEURLHYW', 'RNALEOTISC', 'SETADNLKYH'))\n",
    "assert word_count(lock1240) == 1240\n",
    "\n",
    "def lock_table(locks: Collection[Lock], target=lock1240) -> dict: \n",
    "    \"\"\"A table of {canonical_lock: (word_count, distance_to_target} in sorted order.\"\"\"\n",
    "    locks = sorted(locks, key=word_count, reverse=True)\n",
    "    return {canonical(lock): (word_count(lock), distance(lock, target)) \n",
    "            for lock in locks}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{('BCDFLMPSTW', 'AEHILORUWY', 'ACEILNORST', 'ADEHKLNSTY'): (1240, 0),\n",
       " ('BCDFLMPSTW', 'AEHILOPRUY', 'ACEILNORST', 'ADEHKLNSTY'): (1240, 1),\n",
       " ('BCDFLMPSTW', 'AEHILORUWY', 'ACEILNORST', 'ADEKLMNSTY'): (1236, 1),\n",
       " ('BCDGLMPSTW', 'AEHILORUWY', 'AEILMNORST', 'ADEKLNPSTY'): (1235, 3),\n",
       " ('BCDHLMPSTW', 'AEHILOPRUY', 'ACEILNPRST', 'ADEHKLNSTY'): (1232, 3),\n",
       " ('BCDFHLMPST', 'AEHILORUWY', 'ACELMNORST', 'ADEKLNPSTY'): (1232, 3)}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lock_table(hillclimbs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are far fewer locks than it seemed at first! \n",
    "\n",
    "There are just  two locks (not 21) that score 1240, and they differ in just one letter (a `P` or a `W` in the second disc). \n",
    "\n",
    "This discovery changes my whole thinking about the geometry of the lock/score space.  Previously I imagined a spiky \"porcupine-shaped\" landscape, with many different peaks hitting a height of 1240.  But now I picture a \"space needle\" landscape: a single peak containing the two locks (one with a `P` and one with a `W`), surrounded by other lesser towers. Now I think it is less likely that there is a lock that scores over 1240.\n",
    "\n",
    "# Searching More\n",
    "\n",
    "Despie the revelation, I'm not quite ready to give up on finding a higher-scoring lock. An easy thing to try is to search for 8,000 steps rather than just 4,000:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 31.9 s, sys: 61.3 ms, total: 32 s\n",
      "Wall time: 32.1 s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{('BCDFLMPSTW', 'AEHILOPRUY', 'ACEILNORST', 'ADEHKLNSTY'): (1240, 1),\n",
       " ('BCDFLMPSTW', 'AEHILORUWY', 'ACEILNORST', 'ADEHKLNSTY'): (1240, 0),\n",
       " ('BCDGLMPSTW', 'AEHILORUWY', 'AEILMNORST', 'ADEKLNPSTY'): (1235, 3),\n",
       " ('BCDFHLMPST', 'AEHILORUWY', 'ACELMNORST', 'ADEKLNPSTY'): (1232, 3)}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%time lock_table(hillclimb(lock, repeat=8000) for lock in locks)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That didn't help: still the same two 1240 locks. \n",
    "\n",
    "Maybe part of the problem is that once we've improved a locks a bunch, no single-letter change can improve it further. What if we allowed each change to change either one or two or three letters at a time? That would give us a better chance of escaping from a local maximum."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 1min 51s, sys: 131 ms, total: 1min 51s\n",
      "Wall time: 1min 51s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{('BCDFLMPSTW', 'AEHILOPRUY', 'ACEILNORST', 'ADEHKLNSTY'): (1240, 1),\n",
       " ('BCDFLMPSTW', 'AEHILORUWY', 'ACEILNORST', 'ADEHKLNSTY'): (1240, 0),\n",
       " ('BCDLMPRSTW', 'AEHILORUWY', 'ACEILNORST', 'ADEHKLNSTY'): (1237, 1),\n",
       " ('BCDGLMPSTW', 'AEHILORUWY', 'AEILMNORST', 'ADEKLNPSTY'): (1235, 3),\n",
       " ('BCDHLMPSTW', 'AEHILOPRUY', 'ACEILNPRST', 'ADEHKLNSTY'): (1232, 3)}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def lock_changes(lock, n=3) -> Lock: \n",
    "    \"\"\"Make up to `n` random changes to lock, returning whichever lock is best.\"\"\"\n",
    "    locks = [lock]\n",
    "    for _ in range(n):\n",
    "        locks.append(changed_lock(locks[-1]))\n",
    "    return max(locks, key=word_count)\n",
    "\n",
    "%time lock_table(hillclimb(lock, lock_changes, repeat=3000) for lock in locks)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Again, the same two 1240 locks; it just took longer to run.\n",
    "\n",
    "Maybe we should keep more options open. A hillclimbing search always tracks the single best scoring lock, but a [beam search](https://en.wikipedia.org/wiki/Beam_search) tracks multiple possibilities at each step. The number to track is called the beam width. We can treack 40 different locks, and try up to 3 changes for each one on each iteration:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 1min 50s, sys: 118 ms, total: 1min 50s\n",
      "Wall time: 1min 50s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{('BCDFLMPSTW', 'AEHILORUWY', 'ACEILNORST', 'ADEHKLNSTY'): (1240, 0),\n",
       " ('BCDFLMPSTW', 'AEHILOPRUY', 'ACEILNORST', 'ADEHKLNSTY'): (1240, 1),\n",
       " ('BCDHLMPSTW', 'AEHILORUWY', 'ACEILNORST', 'ADEHKLNSTY'): (1237, 1),\n",
       " ('BCDLMPRSTW', 'AEHILOPRUY', 'ACEILNORST', 'ADEHKLNSTY'): (1237, 2),\n",
       " ('BCDLMPRSTW', 'AEHILORUWY', 'ACEILNORST', 'ADEHKLNSTY'): (1237, 1),\n",
       " ('BCDFLMPSTW', 'AEHILORUWY', 'ACEILNORST', 'ADEKLMNSTY'): (1236, 1),\n",
       " ('BCDHLMPSTW', 'AEHILOPRUY', 'ACEILNORST', 'ADEHKLNSTY'): (1236, 2),\n",
       " ('BCDGLMPSTW', 'AEHILORUWY', 'ACEILNORST', 'ADEHKLNSTY'): (1236, 1),\n",
       " ('BCDFLMPSTW', 'AEHIKLORUY', 'ACEILNORST', 'ADEHKLNSTY'): (1236, 1),\n",
       " ('BCDFLMPSTW', 'AEHILOPRUY', 'ACEILNORST', 'ADEKLNRSTY'): (1236, 2),\n",
       " ('BCDGLMPSTW', 'AEHILORUWY', 'AEILMNORST', 'ADEKLNPSTY'): (1235, 3),\n",
       " ('BCDGLMPSTW', 'AEHILOPRUY', 'ACEILNORST', 'ADEHKLNSTY'): (1235, 2),\n",
       " ('BCDFLMPRST', 'AEHILORUWY', 'ACEILNORST', 'ADEHKLNSTY'): (1235, 1),\n",
       " ('BCDFLMPSTW', 'AEHILORTUY', 'ACEILNORST', 'ADEHKLNSTY'): (1235, 1),\n",
       " ('BCDFLMPSTW', 'AEHILOPRUY', 'ACEILNORST', 'ADEKLMNSTY'): (1235, 2),\n",
       " ('BCDFLMPSTW', 'AEHILORUWY', 'ACEILNORST', 'ADEKLNRSTY'): (1235, 1),\n",
       " ('BCDFLMPRST', 'AEHILOPRUY', 'ACEILNORST', 'ADEHKLNSTY'): (1235, 2),\n",
       " ('BCDFHLMPST', 'AEHILORUWY', 'ACEILNORST', 'ADEHKLNSTY'): (1235, 1),\n",
       " ('BCDFHLMPST', 'AEHILOPRUY', 'ACEILNORST', 'ADEHKLNSTY'): (1234, 2),\n",
       " ('BCDHLMPSTW', 'AEHILORUWY', 'AEILMNORST', 'ADEKLNPSTY'): (1234, 3),\n",
       " ('BCDFLMPSTW', 'AEHILORUWY', 'ACEILNORST', 'ADEKLNPSTY'): (1234, 1),\n",
       " ('BCDGHLMPST', 'AEHILORUWY', 'AEILMNORST', 'ADEKLNPSTY'): (1234, 4),\n",
       " ('BCDFGLMPST', 'AEHILORUWY', 'ACEILNORST', 'ADEHKLNSTY'): (1234, 1),\n",
       " ('BCDFGLMPST', 'AEHILOPRUY', 'ACEILNORST', 'ADEHKLNSTY'): (1233, 2),\n",
       " ('BCDLMPRSTW', 'AEHIKLORUY', 'ACEILNORST', 'ADEHKLNSTY'): (1233, 2),\n",
       " ('BCDFLMPSTW', 'AEHILORUWY', 'ACEILNORST', 'ADEGKLNSTY'): (1233, 1),\n",
       " ('BCDFGLMPST', 'AEHILORUWY', 'ACEILNORST', 'ADEKLMNSTY'): (1233, 2),\n",
       " ('BCDLMPRSTW', 'AEHILOPRUY', 'ACEILNPRST', 'ADEHKLNSTY'): (1232, 3),\n",
       " ('BCDFLMPSTW', 'AEHILOPRUY', 'ACEILNORST', 'ADEKLNPSTY'): (1232, 2),\n",
       " ('BCDLMPRSTW', 'AEHILORTUY', 'ACEILNORST', 'ADEHKLNSTY'): (1232, 2),\n",
       " ('BCDHLMPSTW', 'AEHILOPRUY', 'ACEILNPRST', 'ADEHKLNSTY'): (1232, 3),\n",
       " ('BCDHLMPSTW', 'AEHILORUWY', 'ACEILNPRST', 'ADEHKLNSTY'): (1232, 2),\n",
       " ('BCDFHLMPST', 'AEHILORUWY', 'ACELMNORST', 'ADEKLNPSTY'): (1232, 3),\n",
       " ('BCDHLMPSTW', 'AEHIKLORUY', 'ACEILNORST', 'ADEHKLNSTY'): (1232, 2),\n",
       " ('BCDFHLMPST', 'AEHILORUWY', 'ACEILNORST', 'ADEKLMNSTY'): (1232, 2),\n",
       " ('BCDFGLMPST', 'AEHILORUWY', 'AEILMNORST', 'ADEKLNPSTY'): (1231, 3),\n",
       " ('BCDLMPRSTW', 'AEHILORUWY', 'ACEILNPRST', 'ADEHKLNSTY'): (1231, 2),\n",
       " ('BCDGLMPSTW', 'AEHIKLORUY', 'ACEILNORST', 'ADEHKLNSTY'): (1231, 2),\n",
       " ('BCDFLMPRST', 'AEHIKLORUY', 'ACEILNORST', 'ADEHKLNSTY'): (1231, 2),\n",
       " ('BCDFLMPSTW', 'AEHILORUWY', 'AEILMNORST', 'ADEKLNPSTY'): (1231, 2)}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def beam_search(locks, changer=lock_changes, scorer=word_count, beam_width=40, repeat=3000) -> List[Lock]:\n",
    "    \"\"\"Keep up to `beam_width` locks, changing each one with `changer`, and keeping the best scoring ones\n",
    "    according to `scorer`. Repeat.\"\"\"\n",
    "    locks = set(map(canonical, locks)) # Make a copy\n",
    "    for _ in range(repeat):\n",
    "        locks |= set(map(canonical, map(changer, locks)))\n",
    "        locks = set(sorted(locks, key=scorer, reverse=True)[:beam_width])\n",
    "    return locks\n",
    "\n",
    "%time lock_table(beam_search(locks))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I'm getting more convinced that 1240 is the top-scoring lock. \n",
    "\n",
    "On to the next question."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "# Question 3: Simultaneous Words"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Can we make a lock that spells 10 words simultaneously?  And could a lock with more than 10 letters on a disc spell more than 10 words simultaneously? A disc cannot have duplicates of any letter, so the upper bound is 26.\n",
    "\n",
    "We could use hillclimbing with a `scorer` that counts simultaneous words.  My intuition is that this approach would work, eventually, but that progress would be very slow, because most random changes to a single letter would not increase the number of simultaneous words.\n",
    "\n",
    "An alternative approach is to think of the lock not as a tuple of 4 discs (each with 10 letters), but rather as a list of 10 rows (each with 4 letters), where we want the rows to be words. Then we can just go through the list of valid words and greedily include words that don't duplicate a letter in any column:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "def greedy_simultaneous_words(words=WORDS) -> List[Word]:\n",
    "    \"\"\"Greedily add words that have no duplicate letters with previous rows.\"\"\"\n",
    "    rows = []\n",
    "    for word in words:\n",
    "        if not has_duplicate_letters(word, rows):\n",
    "            rows.append(word)\n",
    "    return rows\n",
    "\n",
    "def has_duplicate_letters(word: Word, rows: List[Word]) -> bool:\n",
    "    \"\"\"Is any letter in this `word` a duplicate of the corresponding letter in any of the `rows`?\"\"\"\n",
    "    return any(word[i] == row[i] for i in range(DISCS) for row in rows)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can run `greedy_simultaneous_words()` (with the help of a `report` function) and see if it gives us 10 (or more) words"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "13 words: AADI BEAD CHEF DIBS EBON FLIC GOGO HUCK ICKY KNUR LYLA ODYL PFFT\n"
     ]
    }
   ],
   "source": [
    "def report(items) -> None: print(len(items), 'words:', *items)\n",
    "    \n",
    "report(greedy_simultaneous_words())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It does give us more than 10 words!\n",
    "\n",
    "The answer we get depends on the order in which we go through the words. So let's try a thousand different orders, shuffling the list of words before each try:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "16 words: BUNN KAPH JOWL ADAM FLUX EPOS GEED RISK MYLA UNCI CRIB ICKY SKYE OTTO THRU PFFT\n"
     ]
    }
   ],
   "source": [
    "def shuffled(items: list) -> List: return random.sample(items, len(items))\n",
    "\n",
    "def greedier_simultaneous_words(words=WORDS, n=1000) -> List[Word]:\n",
    "    \"\"\"Try `n` times and return the try with the most simultaneous words.\"\"\"\n",
    "    tries = (greedy_simultaneous_words(shuffled(WORDS)) for _ in range(n))\n",
    "    return max(tries, key=len)\n",
    "\n",
    "report(greedier_simultaneous_words())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "16 simultaneous words; I think that's pretty good."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Question 4: Coincidence?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There is still one unanswered question: did the designers of WordLock&reg; deliberately put  \"FRED BUNS\" in, or was it a coincidence? Hacker News reader [emhart](https://news.ycombinator.com/user?id=emhart) (aka the competitive lockpicker Schyler Towne) astutely commented that he had found the [patent](https://www.google.com/patents/US6621405) assigned to WordLock; it describes an algorithm similar to my `greedy_lock`.\n",
    "After seeing that, I'm inclined to believe that \"FRED BUNS\" is the coincidental result of running the algorithm. On the other hand, there is a [followup patent](https://www.google.com/patents/US20080053167) that discusses a refinement\n",
    "\"wherein the letters on the wheels are configured to spell a first word displayed on a first row of letters and a second word displayed on a second row of letters.\" So the possibility of a two-word phrase was something that Wordlock LLc. was aware of."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Patents\n",
    "\n",
    "We see below that the procedure described in the [Wordlock Inc patent](https://www.google.com/patents/US6621405) is not quite as good as `greedy_lock`, because the patent states that at each disc position \"*the entire word list is scanned*\" to produce the letter frequencies, whereas `greedy_lock` scans only the *possible* words: the words that are consistent with the previously-filled discs. Because of that difference, the patented algorithm does worse than my `greedy_lock` (by 1,161 to 1,177) and  my `greedier_lock` (by 1,161 to 1,235)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "def greedy_lock_patented(words=WORDS, order=range(DISCS)) -> Lock:\n",
    "    \"\"\"Make a lock where we greedily choose the LETTERS best letters for each disc, in order.\"\"\"\n",
    "    lock = DISCS * [Disc()] # Initially a lock of 4 empty discs\n",
    "    for i in order: \n",
    "        # Make lock[i] be a disc that covers the most words, then update `words`\n",
    "        lock[i] = disc = most_common(words, i)\n",
    "        #### Don't update words #### words = [w for w in words if w[i] in disc]\n",
    "    return Lock(lock)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1161"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "word_count(greedy_lock_patented())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What does it say about our patent system that Wordlock Inc actually got a patent for an algorithm that is worse than the initial idea I came up with just to use as a baseline against the *real* hillclimbing algorithm?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tests"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It is a \n",
    "good idea to have some tests.  The following tests have poor coverage, because it is hard to test non-deterministic functions, and I didn't attempt that here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def tests() -> bool:\n",
    "    assert 'WORD' in WORDS\n",
    "    assert 'FRED' in WORDS\n",
    "    assert 'BUNS' in WORDS\n",
    "    assert 'FIVE' in WORDS\n",
    "    assert 'XYZZ' not in WORDS\n",
    "    assert 'word' not in WORDS\n",
    "    assert 'FIVER' not in WORDS\n",
    "    assert len(WORDS) == 4360\n",
    "\n",
    "    fredbuns = Lock(['FB', 'RU', 'EN', 'DS'])\n",
    "    assert words_from(fredbuns) == ['BRED', 'BUND', 'BUNS', 'FRED', 'FUND', 'FUNS']\n",
    "    assert word_count(fredbuns) == 6\n",
    "    assert most_common(fredbuns, 0) == 'FRED'\n",
    "    assert most_common(['stink', 'stank', 'stunk'], 2) == 'iau'\n",
    "\n",
    "    assert greedy_lock() == ('SPTBDCLMAR', 'OAIEURLHYN', 'RNALEOTISM', 'SETADNLKYP')\n",
    "    assert greedier_lock() == ('BPTCMSDLGW', 'OAEIURLYHW', 'EARNLIOTSM', 'SETNDALKYP')\n",
    "    assert greedy_lock_patented() == ('SPTBDCLMAR', 'AOIEURLHYN', 'EARNLIOTSM', 'SETANDYLKO')\n",
    "    assert word_count(greedier_lock()) >= word_count(greedy_lock())\n",
    "    \n",
    "    assert wordlock == Lock(('SPHMTWDLFB', 'LEYHNRUOAI', 'ENMLRTAOSK', 'DSNMPYLKTE'))\n",
    "    assert regex(wordlock) == '[SPHMTWDLFB][LEYHNRUOAI][ENMLRTAOSK][DSNMPYLKTE]'\n",
    "    assert word_count(wordlock) == 1118\n",
    "    assert canonical(wordlock) == ('BDFHLMPSTW', 'AEHILNORUY', 'AEKLMNORST', 'DEKLMNPSTY')\n",
    "    assert \"FRED\" in words_from(wordlock) \n",
    "    assert \"BUNS\" in words_from(wordlock)\n",
    "    assert \"QUIT\" not in words_from(wordlock)\n",
    "\n",
    "    assert distance(wordlock, lock1240) == 6\n",
    "    assert distance(greedy_lock(), lock1240) == 5\n",
    "    assert distance(greedy_lock(), greedy_lock_patented()) == 1\n",
    "\n",
    "    assert has_duplicate_letters(\"WORD\", [\"WILD\", \"DOGS\"])\n",
    "    assert not has_duplicate_letters(\"WORD\", [\"FREE\", \"CATS\"])\n",
    "\n",
    "    assert greedy_simultaneous_words() == [\n",
    "        'AADI', 'BEAD', 'CHEF', 'DIBS', 'EBON', 'FLIC', 'GOGO', 'HUCK', 'ICKY', 'KNUR', 'LYLA', 'ODYL', 'PFFT']\n",
    "    \n",
    "    assert lock_table([greedy_lock(), greedier_lock(), wordlock]) == {\n",
    "     ('BCDGLMPSTW', 'AEHILORUWY', 'AEILMNORST', 'ADEKLNPSTY'): (1235, 3),\n",
    "     ('ABCDLMPRST', 'AEHILNORUY', 'AEILMNORST', 'ADEKLNPSTY'): (1177, 5),\n",
    "     ('BDFHLMPSTW', 'AEHILNORUY', 'AEKLMNORST', 'DEKLMNPSTY'): (1118, 6)}\n",
    "    \n",
    "    return True\n",
    "\n",
    "tests()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Addendum: 2018\n",
    "\n",
    "New [research](https://www.theverge.com/2018/10/7/17940352/turing-test-one-word-minimal-human-ai-machine-poop) suggests that the one word that humans are most likely to think was generated by another human rather than by a machine is \"*poop*\". Is it a coincidence that the same week this research came out, I was in a bike shop and saw the following:\n",
    "\n",
    "![](like.jpg)\n",
    "\n",
    "This proves that I'm not the only one with a juvenile reaction to the *WordLock*®, but it raises some questions: Was the last visitor to the store a human asserting their individuality? Or a robot yearning to be free? Or [Triumph the insult comic dog](https://en.wikipedia.org/wiki/Triumph_the_Insult_Comic_Dog)? I guess we'll never know."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'POOP' in WORDS"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# One More Question"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I wonder if [@BIKESNOBNYC](https://twitter.com/bikesnobnyc) would appreciate this notebook?  On the one hand, he is the kind of guy who, in discussing the fact that bicycling is the seventh most popular recreational activity,  [wrote]() \"*the number seven is itself a highly significant number. It is the lowest number that cannot be represented as the sum of the square of three integers*,\" so it seems he has some interest in mathematical oddities.  On the other hand, he followed that up by writing \"*I have no idea what that means, but it's true*,\" so maybe not."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{7, 15, 23, 28, 31, 39, 47, 55, 60, 63, 71, 79, 87, 92, 95}"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums = range(11) \n",
    "sums = {A**2 + B**2 + C**2 for A in nums for B in nums for C in nums} # Sums of 3 squares\n",
    "set(range(101)) - sums # Numbers up to 100 that are not the sum of 3 squares"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
